package com.szlanyou.udp_demo;

import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.cz.udpsendermaster.UdpSender;
import com.szlanyou.protocol_service.LogUtil;
import com.szlanyou.protocol_service.ProtocolHandler;
import com.szlanyou.protocol_service.Singleton;
import com.szlanyou.tcp_server_demo.R;
import com.szlanyou.tcpservermaster.util.NetUtil;
import com.szlanyou.udpreceivermaster.UdpReceiver;

public class MainActivity extends AppCompatActivity {
    private Toolbar toolbar;
    private TextView tvRecv;
    private TextView tvMyIp;
    private UdpReceiver udpReceiver;
    private UdpSender udpSender;
    private StringBuilder sb = new StringBuilder();
    private Singleton<ProtocolHandler> protocolHandlerSingleton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initUdpReceiver();
        initUdpSender();
    }

    private void initUdpSender() {
        udpSender = new UdpSender("10.14.207.79", 9999);
        udpSender.setOnSenderStatusListener(new UdpSender.OnSenderStatusListener() {
            @Override
            public void onError(Throwable e) {
                sb.append("发送异常：" + e.getMessage());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tvRecv.setText(sb.toString());
                    }
                });
            }

            @Override
            public void onSendMsgFailure(Throwable e) {
                sb.append("发送失败：" + e.getMessage());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tvRecv.setText(sb.toString());
                    }
                });
            }
        });
    }

    private void initUdpReceiver() {
        initProtocolHandler();

        udpReceiver = new UdpReceiver(9999);
        udpReceiver.listenReceiver(new UdpReceiver.OnReceiverStatusListener() {
            @Override
            public void onReceiverStarted() {
                //启动协议解析
                protocolHandlerSingleton.get().start();
            }

            @Override
            public void onReceiverClosed() {

            }

            @Override
            public void onRead(final byte[] buffer) {
                protocolHandlerSingleton.get().handle(buffer);
            }
        });
        udpReceiver.enableReceiver();



    }

    private void initProtocolHandler() {
        protocolHandlerSingleton = new Singleton<ProtocolHandler>() {
            @Override
            protected ProtocolHandler create() {
                return new ProtocolHandler();
            }
        };
        protocolHandlerSingleton.get().setOnDecodeListener(new ProtocolHandler.OnDecodeListener() {
            @Override
            public void onDecoded(final String oneFrameData) {
                LogUtil.d("收到消息：" + oneFrameData);
                sb.append("<<收到消息：" + oneFrameData + "\n");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tvRecv.setText(sb.toString());
                    }
                });
            }

            @Override
            public void onReadException(Throwable e) {
                LogUtil.e("读取流异常");
                e.printStackTrace();
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        udpReceiver.release();
        protocolHandlerSingleton.get().release();
    }

    private void initView() {
        toolbar = findViewById(R.id.toolbar);
        tvMyIp = findViewById(R.id.tv_my_ip);
        tvRecv = findViewById(R.id.tv_recv);
        initToolBar();

        final String ip = NetUtil.getIp(MainActivity.this);
        tvMyIp.setText("我的IP:" + ip);
    }

    private void initToolBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            toolbar.setTitle(R.string.app_name);
            setSupportActionBar(toolbar);
        }
    }

    public void send(View view) {
        udpSender.send("#{I'm AAAAAAAA}#");
    }

    public void pressureTest(View view) {
        for (int i = 0; i < 500; i++) {
            udpSender.send("#{I'm AAAAAAAA " + i + "}#");
        }
    }
}